package edu.nctu.csie.jichang.database.model.cell;

public class NameMapCell implements Comparable<NameMapCell>{

	public static final String SORT_ASC  ="ASC";
	public static final String SORT_DESC ="DESC";
	
	private String name;                    // 欄位名稱
	private String sort;                    // 排序方式
	private String refTable;
	private String refColumn;
	
	public NameMapCell() {
	}
	
	public NameMapCell(String tColumn) {
		setName(tColumn);
	}
	
	public NameMapCell(String tFkColumnName, String tRefTableName,String tRefColumnName) {
		setName(tFkColumnName);
		setRefTable(tRefTableName);
		setRefColumn(tRefColumnName);
	}

	public NameMapCell(String tColumn, String pSort) {
		setName(tColumn);
		setSort("D".equals(pSort)?SORT_DESC:SORT_ASC);
	}

	

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getRefTable() {
		return refTable;
	}
	public void setRefTable(String refTable) {
		this.refTable = refTable;
	}
	public String getRefColumn() {
		return refColumn;
	}
	public void setRefColumn(String refColumn) {
		this.refColumn = refColumn;
	}

	@Override
	public String toString() {
		if(sort == null){
			if(refTable == null){
				return name;
			}else{
				return name + "=>" + refTable +":" + refColumn;
			}
		}else{
			return name + " " + sort ;
		}
	}

	public int compareTo(NameMapCell pObj) {
		return this.toString().compareTo(pObj.toString());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((refColumn == null) ? 0 : refColumn.hashCode());
		result = prime * result
				+ ((refTable == null) ? 0 : refTable.hashCode());
		result = prime * result + ((sort == null) ? 0 : sort.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		NameMapCell other = (NameMapCell) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (refColumn == null) {
			if (other.refColumn != null)
				return false;
		} else if (!refColumn.equals(other.refColumn))
			return false;
		if (refTable == null) {
			if (other.refTable != null)
				return false;
		} else if (!refTable.equals(other.refTable))
			return false;
		if (sort == null) {
			if (other.sort != null)
				return false;
		} else if (!sort.equals(other.sort))
			return false;
		return true;
	}
}